/**
 * Copyright &copy; 2012-2016 <a href="https://github.com/thinkgem/jeesite">JeeSite</a> All rights reserved.
 */
package com.thinkgem.jeesite.modules.tb_driver.web;


import java.util.Calendar;
import java.util.HashMap;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import com.thinkgem.jeesite.common.config.Global;
import com.thinkgem.jeesite.common.persistence.Page;
import com.thinkgem.jeesite.common.utils.MD5Util;
import com.thinkgem.jeesite.common.utils.OSSUtil;
import com.thinkgem.jeesite.common.utils.StringUtils;
import com.thinkgem.jeesite.common.web.BaseController;
import com.thinkgem.jeesite.modules.tb_driver.entity.TbDriver;
import com.thinkgem.jeesite.modules.tb_driver.service.TbDriverService;

/**
 * 司机管理Controller
 * @author ZhangYangZe
 * @version 2017-05-13
 */
@Controller
@RequestMapping(value = "${adminPath}/tb_driver/tbDriver")
public class TbDriverController extends BaseController {

	@Autowired
	private TbDriverService tbDriverService;
	
	@ModelAttribute
	public TbDriver get(@RequestParam(required=false) String id) {
		TbDriver entity = null;
		if (StringUtils.isNotBlank(id)){
			entity = tbDriverService.get(id);
		}
		if (entity == null){
			entity = new TbDriver();
		}
		return entity;
	}
	
	@RequiresPermissions("tb_driver:tbDriver:view")
	@RequestMapping(value = {"list", ""})
	public String list(TbDriver tbDriver, HttpServletRequest request, HttpServletResponse response, Model model) {
		Page<TbDriver> page = tbDriverService.findPage(new Page<TbDriver>(request, response), tbDriver); 
		model.addAttribute("page", page);
		return "modules/tb_driver/tbDriverList";
	}

	@RequiresPermissions("tb_driver:tbDriver:view")
	@RequestMapping(value = "form")
	public String form(TbDriver tbDriver, Model model) {
		model.addAttribute("tbDriver", tbDriver);
		return "modules/tb_driver/tbDriverForm";
	}

	@RequiresPermissions("tb_driver:tbDriver:edit")
	@RequestMapping(value = "save")
	public String save(TbDriver tbDriver, Model model, RedirectAttributes redirectAttributes) {
		if (!beanValidator(model, tbDriver)){
			return form(tbDriver, model);
		}
		// 如果新密码为空，则不更换密码
			if (StringUtils.isNotBlank(tbDriver.getNewPassword())) {
				tbDriver.setDriverPwd(MD5Util.MD5(tbDriver.getNewPassword()));
			}
		tbDriverService.save(tbDriver);
		addMessage(redirectAttributes, "保存司机成功");
		return "redirect:"+Global.getAdminPath()+"/tb_driver/tbDriver/?repage";
	}
	
	@RequiresPermissions("tb_driver:tbDriver:edit")
	@RequestMapping(value = "delete")
	public String delete(TbDriver tbDriver, RedirectAttributes redirectAttributes) {
		tbDriverService.delete(tbDriver);
		addMessage(redirectAttributes, "删除司机成功");
		return "redirect:"+Global.getAdminPath()+"/tb_driver/tbDriver/?repage";
	}
    /**文件上传
     *
     */
	@RequestMapping(value = "/uploadImg",method = RequestMethod.POST)
	@ResponseBody
	public Map<String, Object> uploadPhoto (
			@RequestParam("myfile")  MultipartFile myfile, 
			@RequestParam(value = "formFieldId", required = false, defaultValue = "pic_url") 
			String formFieldId,
			@RequestParam("DocumentType")String documentType,
			HttpServletRequest request, HttpServletResponse response){
			System.out.println("已选择的证件类型"+documentType);
		    Map<String, Object> result = new HashMap<String, Object>();
		try {
			if (myfile.isEmpty() || myfile==null) {
				result.put("flag" , "0");
				result.put("msg", "图片上传失败");
			} else {
				// 上传图片
				String pathin="";
				String suffixname=myfile.getOriginalFilename();
				String suffix=suffixname.substring(suffixname.indexOf("."),suffixname.length());
				
				if(documentType!=null&&documentType!=""){
					pathin=documentType;
				}
				Calendar c = Calendar.getInstance();
				int year = c.get(Calendar.YEAR);
				int month = c.get(Calendar.MONTH);
				int date = c.get(Calendar.DATE);
				String path = "upload/jianglinerp/"+pathin+"/"+year+"/"+month+"/"+date+"/";
				String upfileName = System.currentTimeMillis()+pathin;
				OSSUtil.uploadFileOfOSS(path + upfileName+suffix,myfile) ;
				result.put("flag", "1");
				result.put("msg", path + upfileName+suffix) ;
			}
		} catch (Exception e) {
			e.printStackTrace();
			result.put("flag" , 0);
			result.put("msg" , "图片上传失败");
		}
		return result;
	}
	
	/**
	 * 根据司机的手机号查询该司机是否已注册
	 * @author ZhaoHai
	 * @version 2017-06-16
	 */
	    @RequestMapping(value = "haverDriver")
	    @ResponseBody
		public TbDriver fingByTell(@RequestParam("tell") String tell) {
	    	System.out.println("前台传入的司机电话"+tell);	    	
			TbDriver entity = null;
			if (StringUtils.isNotBlank(tell)){
				entity = tbDriverService.fingByTell(tell);
			}
			if (entity == null){
				entity = new TbDriver();
			}
			return entity;
		}
}